--
-- Author: xuzhihui
-- Date: 2016-03-15 09:00:00
--
-- 对象基类
local ObjectBase = class("ObjectBase")

function ObjectBase:ctor()
	self.subitems={}

	self._ObjEvents = nil	--每个对象有一个自己的事件系统
end

-- 添加子项
function ObjectBase:AddSubitems(target)
	table.insert(self.subitems, target)
end

--关闭清理
function ObjectBase:ClearData()
	Util.CloseAllFollowViews(self)
	
	if self.subitems then
		for i,v in ipairs(self.subitems) do
			v:ClearData()
		end
	end

	self:RemoveAllMessage()
	self:RemoveAllListener()
end

-- 清理
function ObjectBase:Dispose()
	-- 一步一步修改.
	self:ClearData()
	if self.subitems then
		for i,v in ipairs(self.subitems) do
			v:Dispose()
		end
		self.subitems = {}
	end
	MeTools.RemoveObjHierarchyFromMe(self, self)
	MeTools.RemoveObjUIAnimationTweenFromMe(self, self)
end

function ObjectBase:RemoveAllMessage()
	--释放自己绑定的消息
	--清空自己注册的消息
    SoraDRemoveMessageByTarget(self)
    -- 移除绑定的定时器
    SoraDSendMessage({msg = "MESSAGE_FRAMEWORK_REMOVE_TIMER_FROM_TARGET", target = self})
    -- 清理头像管理事件
    HeadManager.RemoveListener(self)
end

--eventid 每个对象自己定义，对象内部不重复就ok了
function ObjectBase:AddEventListener(msg, target, listener)
	if not listener then
		return 
	end

	if not self._ObjEvents then
		self._ObjEvents = {}
	end

	local id = getMessageByDefine(msg)


	local lstListener = self._ObjEvents[id]
	if not lstListener then
		lstListener = {}
		self._ObjEvents[id] = lstListener
	end

	local lstTarget = lstListener[listener]
	if not lstTarget then
		lstTarget = {}
		lstListener[listener] = lstTarget
	end

	local val = target or 1
	if not self:IfTableContains(lstTarget, val) then
		table.insert(lstTarget, val)
	end
	
end

function ObjectBase:IfTableContains(tb, val)
	for k,v in ipairs(tb) do 
		if v==val then
			return true
		end
	end

	return false
end

function ObjectBase:RemoveEventListener(msg, listener)
	if not listener or not self._ObjEvents then
		return 
	end
	local id = getMessageByDefine(msg)
	local lstListener = self._ObjEvents[id]
	if lstListener and lstListener[listener] then
		lstListener[listener] = nil
	end
end

function ObjectBase:RemoveEventTargetListener(msg, listener, target)
	if not listener or not self._ObjEvents then
		return 
	end

	target = nil or 1

	local id = getMessageByDefine(msg)
	local lstListener = self._ObjEvents[id]
	if lstListener and lstListener[listener] then
		local lstTarget = lstListener[listener]
		for k,v in ipairs(lstTarget) do
			if v==target then
				table.remove(lstTarget, k)
				break
			end
		end
	end
end

function ObjectBase:SendEventMessage(msg, ...)
	if not self._ObjEvents then
		return
	end 

	local id = getMessageByDefine(msg)
	local lstListener = self._ObjEvents[id]

	if not lstListener then
		return 
	end

	for func,v in pairs(lstListener) do
		for k,target in ipairs(v) do
			if target==1 then
				func(...)
			else
				func(target, ...)
			end
		end
	end
end

function ObjectBase:RemoveAllListener()
	self._ObjEvents = nil
end

-- 创建加密属性
function ObjectBase:AddEncryptField(key, value)
	value = value or 0
	if key and type(key) == "string" and type(value) == "number" then
		EncryptFields(self, {[key] = value})
	else
		printError("只能加密数字>%s:%s", key, value)
	end
end

-- 删除加密属性
function ObjectBase:RemoveEncryptField(key)
	if key and self.__fields and self.__fields[key] then
		table.remove(self.__fields, key)
	end
end

-- 获取加密属性值
function ObjectBase:GetEncryptField(key)
	if key and self.__fields and self.__fields[key] then
		return self.__fields[key]
	end
end

return ObjectBase